package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketAdmin;

import com.cskaoyan.market.service.AdminAdminService;
import com.cskaoyan.market.service.impl.AdminAdminServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;

import java.util.List;





@WebServlet("/admin/admin/*")
public class AdminAdminController extends HttpServlet {
    AdminAdminService adminAdminService=new AdminAdminServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/admin/", "");
        if("list".equals(op)){
            list(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/admin/", "");
        if("create".equals(op)){
            create(req,resp);
        }
        if("delete".equals(op)){
            delete(req,resp);
        }
        if("update".equals(op)){
            update(req,resp);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketAdmin marketAdmin = objectMapper.readValue(req.getReader().readLine(), MarketAdmin.class);
        marketAdmin.setUpdateTime(LocalDateTime.now());
        int effective = adminAdminService.update(marketAdmin);
        String respJson=null;
        if(effective == 1){
            respJson=  JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(marketAdmin));
        }
        else{
            respJson= JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badUpdate());
        }

        resp.getWriter().println(respJson);

    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketAdmin marketAdmin = objectMapper.readValue(req.getReader().readLine(), MarketAdmin.class);

        int effective = adminAdminService.delete(marketAdmin);
        String respJson=null;
        if(effective == 1){
            respJson=  JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok());
        }
        else{
            respJson= JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badDelete());
        }

        resp.getWriter().println(respJson);

    }


    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketAdmin marketAdmin = objectMapper.readValue(req.getReader().readLine(), MarketAdmin.class);
        marketAdmin.setAddTime(LocalDateTime.now());
        marketAdmin.setUpdateTime(LocalDateTime.now());
        int effective = adminAdminService.create(marketAdmin);
        String respJson=null;
        if(effective == 1){
            respJson=  JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(marketAdmin));
        }
        else{
            respJson= JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badCreate());
        }

        resp.getWriter().println(respJson);

    }


    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");

        List<MarketAdmin> list= adminAdminService.list(Integer.parseInt(page),
                Integer.parseInt(limit), sort, order);
        String Json=listJson(list);
        resp.getWriter().println(Json);
    }

    private String listJson(List<MarketAdmin> list) throws JsonProcessingException {
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();


        if(list==null||list.size()==0){
            return objectMapper.writeValueAsString(ResponseUtil.badList());
        }
        return objectMapper.writeValueAsString(ResponseUtil.okList(list));
    }
}
